package com.dk.controller;

import com.dk.pojo.Role;
import com.dk.pojo.User;
import com.dk.service.RoleService;
import com.dk.service.RoleUserService;
import com.dk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    RoleUserService roleUserService;

    @RequestMapping("getInfo")
    @ResponseBody
    public List<User> getInfo() {
        List<User> userList = userService.getInfo();
        return userList;
    }
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<Role> roles = roleService.getInfo();
        model.addAttribute("roles",roles);
        return "user/add";
    }
    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(@RequestParam("rid")Integer[] rid, User user){
        int i = userService.insertInfo(rid,user);
        return i>0;
    }
    @RequestMapping("toUpdate/{uid}")
    public String toUpdate(@PathVariable("uid")Integer uid, Model model){
        List<Role> roles = roleService.getInfo();
        model.addAttribute("roles",roles);
        User user = userService.getInfoByUid(uid);
        model.addAttribute("user",user);
        List<Integer> rids = roleService.getInfoByUid(uid);
        model.addAttribute("rids",rids);
        return "user/update";
    }
    @RequestMapping("updateUserInfo")
    @ResponseBody
    public boolean updateUserInfo(User user,@RequestParam("rid")Integer[] rid){
        int i = userService.updateUserInfo(user,rid);
        return i>0;
    }
    @RequestMapping("deleteInfoByUid/{uid}")
    @ResponseBody
    public boolean deleteInfoByUid(@PathVariable("uid")Integer uid){
        int i = userService.deleteInfoByUid(uid);
        return i>0;
    }

}
